package org.phone.web.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.phone.manager.dao.MySQLOperate;
import org.phone.manager.entity.CompanyInfo;
import org.phone.manager.entity.Product;
import org.phone.manager.entity.ProductType;
import org.phone.manager.services.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = { "/", "/home" })
public class HomeController extends ParentController {

	@Autowired
	@Qualifier("productService")
	private ProductService ps;

	@RequestMapping(method = RequestMethod.GET)
	public String showHome(Model model) {
		List<Product> productList = ps.getAllProducts();
		List<List> typeList = ps.getAllType();
		CompanyInfo companyInfo = (CompanyInfo) dao.getEntity(CompanyInfo.class, 1);
		model.addAttribute("productList", productList);
		model.addAttribute("firstTypeList", typeList.get(0));
		model.addAttribute("secondTypeList", typeList.get(1));
		model.addAttribute("product", new Product());
		if (companyInfo == null) {
			model.addAttribute("companyInfo", new CompanyInfo());
		} else {
			model.addAttribute("companyInfo",companyInfo);
		}
		return "home";
	}

	// 保存修改产品信息
	@RequestMapping(value = { "/", "/home" }, method = RequestMethod.POST, params = "new")
	public String addProduct(Product product, @RequestParam("secondName") Integer id) {
		ps.saveOrUpdatePrduct(product, id);
		return "redirect:/home";
	}

	// 显示修改产品界面
	@RequestMapping(value = "/updateProduct", method = RequestMethod.GET)
	public String showEditType(@RequestParam("id") Integer id, Model model) {
		Product product = (Product) ps.getEntity(Product.class, id);
		List<List> typeList = ps.getAllType();
		model.addAttribute("product", product);
		model.addAttribute("firstTypeList", typeList.get(0));
		model.addAttribute("secondTypeList", typeList.get(1));
		return "editproduct";
	}

	// 删除产品信息
	@RequestMapping(value = "/delProduct", method = RequestMethod.GET, params = "id")
	public String deleteCategory(@RequestParam("id") Integer id) {
		Product prepareDelete = (Product) ps.getEntity(Product.class, id);
		ps.deleteEntity(prepareDelete);
		return "redirect:/home";
	}

	// 查询产品信息
	@RequestMapping(value = "/serch", method = RequestMethod.GET)
	@Transactional
	public String showProdcut(@RequestParam("serchId") Integer id, @RequestParam("keyValue") String keyValue,
			Model model) {
		Set<Product> productList;
		if (id == null && keyValue.isEmpty())
			return showHome(model);
		if (id != null && keyValue.isEmpty()) {
			productList = ps.getProductInfo(id);
			showHome(model);
			model.addAttribute("productList", productList);
		}
		if (id == null && !keyValue.isEmpty()) {
			List<Product> productListkey = ps.getProductInfo(keyValue);
			showHome(model);
			model.addAttribute("productList", productListkey);
		}

		if (id != null && !keyValue.isEmpty()) {
			List<Product> productListkey = ps.getProductInfo(id, keyValue);
			showHome(model);
			model.addAttribute("productList", productListkey);
		}
		return "home";

	}

}
